package com.atguigu.edu2021.mock.stage;

import com.atguigu.edu2021.mock.bean.CartInfo;
import com.atguigu.edu2021.mock.bean.CourseInfo;
import com.atguigu.edu2021.mock.bean.FavorInfo;
import com.atguigu.edu2021.mock.config.AppConfig;
import com.atguigu.edu2021.mock.factory.UserSession;
import com.atguigu.edu2021.mock.log.*;
import com.atguigu.edu2021.mock.log.enums.ActionId;
import com.atguigu.edu2021.mock.log.enums.ItemType;
import com.atguigu.edu2021.mock.log.enums.PageId;
import com.atguigu.edu2021.mock.service.*;
import com.atguigu.mock.util.LogUtil;
import com.atguigu.mock.util.RandomCollection;
import com.atguigu.mock.util.RandomNum;
import com.atguigu.mock.util.RandomBox;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

@Component("courseDetail")
public class CourseDetailStage implements Stage {

    @Autowired
    OrderInfoService orderInfoService;

    @Override
    public boolean handle() {
        CourseInfo courseInfo =   courseInfoService.getOneByRandom();

        AppMain appMain =  mainLog(courseInfo);

        handleFavor(appMain,courseInfo );

        tryBuyCourse(appMain,courseInfo);

        LogUtil.logToJson(appMain);

        UserSession.setLastPageId(PageId.course_detail);
        UserSession.setCurCourseInfo(courseInfo );
        return true;
    }

    @Autowired
    CourseInfoService courseInfoService;

    @Autowired
    CartInfoService cartInfoService;

    @Autowired
    FavorInfoService favorInfoService;

    @Autowired
    ReviewInfoService reviewInfoService;

    private CourseInfo getCourseInfoByPurpose(){
        List<CourseInfo> orderCourseList = orderInfoService.getOrderCourseList(UserSession.getUserInfo().getId());

        if(UserSession.getPurpose().equals(UserSession.PURPOSE_STUDY)){
               return RandomCollection.getOneFrom(orderCourseList);
         }else{   //如果是访问浏览  则不会再访问已经购买过的商品
            List<Long> existsCourseIdList=new ArrayList<>();
            for (CourseInfo courseInfo : orderCourseList) {
                existsCourseIdList.add(courseInfo.getId());
            }
             Map<Long, CourseInfo> courseInfoMap = courseInfoService.all(true);
            CourseInfo courseInfo=null;
            if(courseInfoMap.size()!=0){
                courseInfo = RandomCollection.getOneFrom(courseInfoMap, existsCourseIdList);
            }

            return  courseInfo;

        }
    }




    private  AppMain mainLog( CourseInfo courseInfo){
        AppCommon appCommon = UserSession.getAppCommon();

        Long courseId = courseInfo.getId();
        Integer durTime= RandomNum.getRandInt(5000,AppConfig.page_during_max_ms);

        AppPage appPage = AppPage.builder()
                .last_page_id(UserSession.getLastPageId())
                .item(courseId.toString())
                .item_type(ItemType.course_id)
                .during_time(durTime)
                .page_id(PageId.course_detail).build();


        List<AppDisplay> appDisplayList = AppDisplay.builder().buildList();

        UserSession.addTimeByDuringTime(durTime);
        AppMain appMain = AppMain.builder().common(appCommon).page(appPage).displays(appDisplayList).ts(UserSession.getCurDateTime().getTime()).checkError().build();
        return appMain;
    }


    private void   tryBuyCourse( AppMain appMain,CourseInfo courseInfo ){
        List<CourseInfo> orderCourseList = orderInfoService.getOrderCourseList(UserSession.getUserInfo().getId());
        for (CourseInfo orderCourse : orderCourseList) {
            if (orderCourse.getId()==courseInfo.getId()){
                return   ;
            }
        }


        if( UserSession.getNextStageInfo().equals(CartStage.class)) {
            List<CartInfo> cartList = UserSession.getCartList();
            if (cartList!=null){
                for (CartInfo cartInfo : cartList) {
                    if(cartInfo.getCourseId()==courseInfo.getId()){
                        return;
                    }
                }
            }
            CartInfo cartInfo = genCart(courseInfo);
            AppAction cartAction = new AppAction(ActionId.cart_add, ItemType.course_id, cartInfo.getCourseId().toString(),UserSession.getCurDateTime().getTime());
            appMain.addAction(cartAction);

            cartInfoService.saveOrUpdate(cartInfo);
        }else if( UserSession.getNextStageInfo().equals(OrderStage.class)){
            UserSession.setBuyingCourseInfo(courseInfo);
        }

    }

    // 添加购物车
    private  void   handleCart(AppMain appMain, CourseInfo courseInfo ){



    }


    //生成购物车
    public CartInfo genCart(CourseInfo courseInfo){

            CartInfo cartInfo = new CartInfo();
            cartInfo.setCourseName(courseInfo.getCourseName());
            cartInfo.setCourseId(courseInfo.getId());
            cartInfo.setCartPrice(courseInfo.getActualPrice());
            cartInfo.setUserId(UserSession.getUserInfo().getId());
            cartInfo.setCreateTime(UserSession.getCurDateTime());
            cartInfo.setSessionId(UserSession.getSessionId());
            cartInfo.setDeleted("0");
            cartInfo.setSold("0");
            UserSession.addCart(cartInfo);
            return cartInfo;

    }



    // 添加收藏
    private  void   handleFavor(AppMain appMain, CourseInfo courseInfo ){
        Boolean ifFavor=  RandomBox.builder().add(true ,AppConfig.if_favor_rate)
                .add(false,100-AppConfig.if_cart_rate).build().getRandBoolValue();
        if(ifFavor){
            FavorInfo favorInfo = genFavor(courseInfo);
            favorInfoService.saveOrUpdate(favorInfo);

            AppAction favorAction = new AppAction(ActionId.favor_add, ItemType.course_id, favorInfo.getCourseId().toString(), UserSession.getCurDateTime().getTime());
            appMain.addAction(favorAction);

        }

    }


    public FavorInfo genFavor(CourseInfo courseInfo){

        FavorInfo favorInfo = new FavorInfo();
        favorInfo.setCourseId(courseInfo.getId());
        favorInfo.setUserId(UserSession.getUserInfo().getId());
        favorInfo.setCreateTime(UserSession.getCurDateTime());
        favorInfo.setDeleted("0");
        return favorInfo;

    }



}
